package com.cwj.lab3;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class Graph_matrix {
    private int[][]matrix;//邻接矩阵
    private int V_num;//点数
    private int E_num;//边数
    private final int INF=Integer.MAX_VALUE;//无穷
//建树
    public void init(int v_num, int e_num) {
        V_num = v_num;
        E_num = e_num;
        matrix=new int[V_num][V_num];
        for (int i = 0; i < V_num; i++) {
            for (int j = 0; j < V_num; j++) {
                if(i!=j){
                    this.matrix[i][j]=INF;
                }else{
                    this.matrix[i][j]=0;
                }
            }
        }
    }
    //利用文件建树
    public void initUnGraph(String path) {
        int u=0,v=0,val=0;
        try(Scanner in=new Scanner(new File(path))){
            init(in.nextInt(),in.nextInt());
            for (int i = 0; i < E_num; i++) {
                u=in.nextInt();
                v=in.nextInt();
                val=in.nextInt();
                this.matrix[u][v]=val;
                this.matrix[v][u]=val;
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }
    }
    public int getVal(int i,int j){
        return this.matrix[i][j];
    }
    //输出图
    @Override
    public String toString() {
        StringBuffer sb=new StringBuffer();
        sb.append("Graph_martix:\n");
        for (int i = 0; i < V_num; i++) {
            sb.append(Arrays.toString(matrix[i]));
            sb.append("\n");
        }
        return sb.toString();
    }

    public int getV_num() {
        return V_num;
    }

    public int getE_num() {
        return E_num;
    }

    public int getINF() {
        return INF;
    }

    public static void main(String[] args) {
        String path="src/src/com/cwj/lab3/UndirectedGraph_data";
        Graph_matrix graph=new Graph_matrix();
        graph.initUnGraph(path);
        System.out.println(graph.toString());
        DFS dfs=new DFS();
        BFS bfs=new BFS();
        Prim prim=new Prim();
        Kruskal kru=new Kruskal();
        Dijkstra dij=new Dijkstra();
        System.out.println("dfs:"+dfs.DFSString(graph,3));
        System.out.println("bfs:"+bfs.BFSTaverl(graph,3));
        System.out.println("Prim:"+prim.getSmallestTree(graph,0));
        System.out.println("Kruskal:"+kru.getSmallestTree(graph));
        System.out.println("Dijkstra:\n");dij.smallestPath(graph,0);
        dij.toList();
    }
}
